<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html style="overflow: auto;">
<head>
    <title>植百汇入库单</title>
    <script type="text/javascript" src="${ctxStatic}/plugin/layui2.0/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="${ctxStatic}/plugin/layui2.0/layui.js"></script>
    <link rel="stylesheet" href="${ctxStatic}/plugin/layui2.0/css/layui.css" media="all">
    <script>
        var ctx = '${ctx}';
    </script>
    <script src="${ctxStatic}/common/js/jeeplus.js"></script>
    <script>
        function findWarehouseType(warehouseType,arr){
            $.ajax({
                type : "post",
                url : ctx+"/sys/dict/findWarehouseTypeByTypeList?typeList="+arr, //此次url改为真正需要的url
                success : function(data) {
                    var optionstring = "<option value=''>"+"--请选择--"+"</option>";
                    if(data!=null){
                        for(var i=0;i<data.length;i++){
                            optionstring += "<option value=\"" + data[i].value+ "\">" +data[i].label+ "</option>";
                        }
                    }
                    $("#"+warehouseType).html(optionstring);
                    layui.use('form', function () {

                        var form = layui.form;
                        form.render();

                    });
                }
            });
        }

        function findWarehouse(warehouseId,warehouseType) {
            $("#"+warehouseId).empty();
            var type=$("#"+warehouseType).val();
            $.ajax({
                type : "post",
                url : ctx+"/warehouse/warehouse/findByType?warehouseType="+type, //此次url改为真正需要的url
                success : function(data) {
                    var optionstring = "";
                    if(data!=null){
                        for(var i=0;i<data.length;i++){
                            optionstring += "<option value=\"" + data[i].id+ "\">" +data[i].warehouseName+ "</option>";
                        }
                    }
                    $("#"+warehouseId).html(optionstring);
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render();
                    });
                }
            });
        };
    </script>
</head>
<body style="width:100%; height:100%; overflow: scroll; -webkit-overflow-scrolling: touch;">
<div class="wrap">

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>入库单(手动)</legend>
</fieldset>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">仓库类型:</label>
            <div class="layui-input-inline">
                <select model="select" id="warehouseType" lay-verify="required"  lay-filter="warehouseType"  lay-search="warehouseType">
                    <option value="">直接选择或搜索选择</option>
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">仓库:</label>
            <div class="layui-input-inline">
                <select model="select"  id="warehouse" name="warehouse" lay-verify="required" lay-search="">
                    <option value="">直接选择或搜索选择</option>

                </select>
            </div>
        </div>

    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea id="remarks" name="remarks" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>入库单明细<div onclick="sub()" class="layui-btn layui-btn-normal">选择sku</div></legend>
    </fieldset>

    <div class="layui-form-item" id="item" style="display:none">
        <div class="layui-inline">
            <label class="layui-form-label">SKU</label>
            <div style="" class="layui-input-inline">
                <input  sku=""  readonly value=""  type="text" id="sku" name="sku" autocomplete="off"
                        placeholder="" class="layui-input"/>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">采购单价</label>
            <div class="layui-input-block">
                <input type="text" id="price" onkeyup="value=value.replace(/[^\d\.]/g,'')"
                       onblur="value=value.replace(/[^\d\.]/g,'')" name="price" autocomplete="off"
                       placeholder="请输入采购单价" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">采购数量</label>
            <div class="layui-input-block">
                <input type="text" onkeyup='this.value=this.value.replace(/\D/g,&apos;&apos;)' name="num" id="num"
                       autocomplete="off" placeholder="请输入采购数量" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <div onclick="removeItem(this)" class="layui-btn  layui-btn-danger ">删除</div>
            </div>
        </div>
    </div>

    <div id="purchaseOrder">
    </div>


    <hr/>
    <div class="layui-form-item" style="text-align:center">
        <div class="layui-inline">
            <div class="layui-input-block">
                <div onclick="complete()" class="layui-btn" >确认入库</div>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <div onclick="reBack()" class="layui-btn layui-btn-primary">关闭</div>
            </div>
        </div>
    </div>
</form>
    
</div>
</body>

<script>
    findWarehouseType("warehouseType",[]);
</script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        form.on('select(warehouseType)', function(data){
            findWarehouse('warehouse','warehouseType');
            form.render();
        });
    });
</script>
<script>


    function complete() {
        layer.confirm('确认入库后将会生成资源码,并且不可改?', {
                btn: ['确定', '取消']
            }, function (index, layero) {
                subForm();
            }
        );
    }



    function subForm() {
        var purchaseOrder=new Object();
        /* purchaseOrder.remarks=$("#remarks").val();//备注
        purchaseOrder.warehouseId=$("#warehouse").val();//付款方式 */

        if(""==$("#warehouse").val()){
            layer.alert('仓库没有选择!', {
                icon: 2,
                title: "提示"
            });
            return ;
        }

        //采购明细单
        var purchaseOrderItems=$("#purchaseOrder").find(".layui-form-item");
        var orderItems=new Array();
        var orderItem;
        for(var i=0;i<purchaseOrderItems.length;i++){
            orderItem=new Object();
            orderItem.skuId=  $(purchaseOrderItems[i]).find("#sku").attr("sku");
            orderItem.price=$(purchaseOrderItems[i]).find("#price").val();
            orderItem.num=$(purchaseOrderItems[i]).find("#num").val();

            if(orderItem.skuId=="" || orderItem.price=="" || orderItem.num==""){
                layer.alert('有空数据请检查!', {
                    icon: 2,
                    title: "提示"
                });
                return ;
            }

            orderItems.push(orderItem);
        }
        purchaseOrder.items=orderItems;
        var fromData=new FormData();

      //  fromData.append("resourceCode","RECEIPT");
        fromData.append("jsons",JSON.stringify(orderItems));
        fromData.append("remarks",$("#remarks").val());
        fromData.append("warehouseId",$("#warehouse").val());
       // alert(JSON.stringify(purchaseOrder));
        //提交数据
        $.ajax({
            url:"${ctx}/warehouse/warehouseReceipt/save",
            type:"post",
            data:fromData,
            contentType: false, // 注意这里应设为false
            processData: false,
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#fff'] //0.1透明度的白色背景
                });
            },
            complete: function () {
                layer.close(index);
            },
            success:function(data){

                if(data.message=="0"){
                    layer.alert('保存成功!', { icon: 1, closeBtn: 0 }, function (index) {
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);//关闭页面
                        parent.$('#table').bootstrapTable('refresh');  //刷新table
                    });
                }else if(data.message==1){
                    layer.alert(data.data, {
                        icon: 2,
                        title: "提示"
                    });

                }
            },
            error:function(e){
                layer.alert('保存失败!', {
                    icon: 2,
                    title: "提示"
                });

            }
        });



    }

    function reBack() {
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);//关闭页面
    }
</script>
<script type="text/javascript">
    //弹窗选择sku
    function sub() {
        layer.open({
            type: 2,
            title: 'sku选择',
            maxmin: true, //开启最大化最小化按钮*!/
            area: ['96%', '90%'],
            offset: '10px',
            content:ctx+"/sku/sku/pop",
            btn:['确定','取消'],
            yes : function(index,layero) {

                var rows=$(layero).find("iframe")[0].contentWindow.$("#skuTable").bootstrapTable('getSelections');

                if(null!=rows){
                    for(var i=0;i<rows.length;i++){
                        var skuName=rows[i].productName+"：" + "["+jp.getDictLabel(${fns:toJson(fns:getDictList('sc_category_a'))}, rows[i].categoryA, "-")+"]" +"—" +"["+jp.getDictLabel(${fns:toJson(fns:getDictList('sc_category_b'))}, rows[i].categoryB, "-")+"]" +"—" +"["+jp.getDictLabel(${fns:toJson(fns:getDictList('sc_category_c'))}, rows[i].categoryC, "-")+"]";
                        var res=checkSkuName(rows[i].id);//校验是否重复
                        if(res==0){
                            addItem(skuName,rows[i].id);
                        }

                    }
                }
            }
        });
    }
    //校验sku是否重复
    function checkSkuName(id) {
        var k=0;
        var skus=$("#purchaseOrder").find("input[name='sku']");
        for(var i=0;i<skus.length;i++){
            if(id==$(skus[i]).attr("sku")){
                layer.alert('已存在相同的sku!', {
                    icon: 3,
                    title: "提示"
                });
                k=1;
            }
        }
        return k;
    }

    //添加
    function addItem(skuName,skuId) {
        var sku=$("#item").find("input[name='sku']");
        $(sku).attr("value",skuName);
        $(sku).attr("sku",skuId);
        var html="<div class=\"layui-form-item\" >"+$("#item").html()+"</div>";
        $("#purchaseOrder").append(html);
    }

    //删除
    function removeItem(obj) {
        $(obj).parent().parent().parent().remove();
    }

</script>
</html>